<template>
  <fileSearchView
    :tableData="tableData"
    :total="total"
    :pageSize="pageSize"
    :currentPage="currentPage"
    :fileSeach="fileSeachNew"
    @changePage="changePage"
    @sizeChange="sizeChange"
    @findFile="findFile"
    @rowClick="rowClick"
  ></fileSearchView>
</template>
<script>
  import fileSearchView from './fileSearchView';

  export default {
    components: {
      fileSearchView
    },
    name: "fileSearchLogic",
    props: {},
    filters: {},
    computed: {},
    watch: {},
    data() {
      return {
        tableData: [], // 文件列表
        total: 0, // 总数
        pageSize: 10, // 每页条数
        currentPage: 1, // 当前页
        fileSeachNew: "", // 搜索框内容
        tableDataSum: [], // 存储全部的文件列表用于分页用
        rowClickStatus: true // 点击行的状态防止多次点击
      }
    },
    created() {
      let obj = {
        dri: this.fileSeachNew
      };
      this.getfile(obj);
    },
    mounted() {

    },
    activated() {
      let obj = {
        dri: this.fileSeachNew
      };
      this.getfile(obj);
    },
    methods: {
      /**
       * 获取文件路径
       * @param obj {dri: ""} 路径
       */
      getfile(obj) {
        _https.post.getHdfs(obj).then(res => {
          if (res.status == 200) {
            this.tableDataSum = res.data == "" ? [] : res.data.FileStatuses.FileStatus;
            let currentPage = this.currentPage;
            if (res.data.length % this.pageSize == 0 && currentPage != 1) {
              currentPage = Math.ceil(res.data.length / this.pageSize);
              this.currentPage = currentPage;
            }
            this.setDataList(currentPage);
            this.total = res.data == "" ? 0 : res.data.length;
          }
        })
      },
      /**
       * 被点击的当前行
       * @param row 行信息
       */
      rowClick(row) {
        if (this.rowClickStatus) {
          this.rowClickStatus = false;
          setTimeout(() => {
            this.rowClickStatus = true;
          }, 500);
          if (row.type === "FILE") {
            this.$message({
              type: 'warning',
              message: '已经是最底层了!'
            });
            return;
          }
          this.fileSeachNew += `${row.pathSuffix}/`;
          let obj = {
            dri: this.fileSeachNew
          };
          this.getfile(obj);
        }
      },
      // 路径
      findFile(inFileSeach) {
        let obj = {
          dri: inFileSeach
        }

        _https.post.getHdfs(obj).then(res => {
          if (res.status == 200) {
            if (res.data.FileStatuses.FileStatus[0].type === "FILE") {
              this.$message({
                type: 'warning',
                message: '已经是最底层了!'
              });
              return;
            }
            this.tableDataSum = res.data == "" ? [] : res.data.FileStatuses.FileStatus;
            let currentPage = this.currentPage;
            if (res.data.length % this.pageSize == 0 && currentPage != 1) {
              currentPage = Math.ceil(res.data.length / this.pageSize);
              this.currentPage = currentPage;
            }
            this.setDataList(currentPage);
            this.total = res.data == "" ? 0 : res.data.length;
          }
        })

        this.fileSeachNew = inFileSeach;
      },
      /**
       * 将返回的数据进行分页操作
       * @param currentPage {Number}
       */
      setDataList(currentPage) {
        let current = currentPage * this.pageSize;
        let currentStart = current - this.pageSize;
        let currentEnd = current;
        this.tableData = this.tableDataSum.slice(currentStart, currentEnd);
      },
      /**
       * 改变当前页面的时候
       * @param page 当前页
       */
      changePage(page) {
        this.currentPage = page;
        this.setDataList(page);
      },
      /**
       * 改变每页条数的回调
       * @param num 每页条数
       */
      sizeChange(num) {
        this.pageSize = num;
        let obj = {
          dri: this.fileSeachNew
        }
        this.getfile(obj);
      }
    },
    destroyed() {

    }
  }
</script>

<style scoped>

</style>
